package com.wl.studyjob.dbchange.zk;

import org.I0Itec.zkclient.ZkClient;

import java.util.Properties;

public class ServerDb {

    ZkClient zkClient = null;
    //1.首先获取zk连接信息
    private void getConnectZK(){
        zkClient = new ZkClient("linux121:2181,linux122:2181,linux123:2181");

        if(!zkClient.exists("/dbservers")){
            zkClient.createPersistent("/dbservers");
        }
    }


    //2. 向zk注册信息
    private void saveServerInfo(){

        Properties properties = new Properties();
        properties.setProperty("driverClassName","com.mysql.jdbc.Driver");
        properties.setProperty("url","jdbc:mysql://192.168.198.123:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false");
        properties.setProperty("username","root");
        properties.setProperty("password","12345678");
        zkClient.createPersistent("/dbservers/db", properties);

        System.out.println("---->>> 将数据库连接信息交给Zookeeper配置中心管理，节点路路径信息="
                + "/dbservers/" + zkClient.getChildren("/dbservers" ).get(0));
    }

    public static void main(String[] args) {
        ServerDb serverDb = new ServerDb();

        serverDb.getConnectZK();
        serverDb.saveServerInfo();

    }


}
